/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SourceCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author ssaad
 */
public class Item implements Serializable
{

    private String title;
    private  int Checkoutlength;
    public ArrayList<Reservation> reservations ;
    public ArrayList<Copy> Copies ;

    public Item(String title, int Checkoutlength )
    {
        this.title = title;
        this.Checkoutlength = Checkoutlength;
        Copies = new ArrayList<Copy>();
        reservations = new ArrayList<Reservation>();
    }

    public int getCheckoutlength() {
        return Checkoutlength;
    }

    public String getTitle() {
        return title;
    }

    public void addCopy(Copy copy)
    {
        this.Copies.add(copy);
    }

    public void removeCopy(Copy copy)
    {
       this.Copies.remove(copy);
    }
    public boolean HaveCopy()
    {
        if(Copies.size() >0)
            return true;
        else
            return false;
    }
    public boolean Isreserved()
    {
        if(reservations.size() >0)
            return true;
        else
            return false;
    }
    /*
     * Return ArrayList<date> of Item Copies returnDates
     */
    public ArrayList<Date> Availableon()
    {
        ArrayList<Date> dates = new ArrayList<Date>();
        for(Copy copy : Copies)
        {
            if(copy.loan != null)
            {
                dates.add(copy.loan.getReturnDate());
            }
        }
        return dates;
    }
    public int AvailabeCopy()
    {
        int availablecopiesno=0;
        for(Copy copy : Copies)
        {
            if(copy.IsAvailable())
            {
                availablecopiesno +=1;
            }
        }
        return availablecopiesno;
    }
    




    public void setCheckoutlength(int Checkoutlength) {
        this.Checkoutlength = Checkoutlength;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String tostring()
    {
        return "Title: " + this.title + "  Checkout Length " + this.Checkoutlength;
    }

    /*
     * return Member list of  reservation list of one item
     */
    public ArrayList<Member> MemberReservationList()
    {
        ArrayList<Member> members = new ArrayList<Member>();

        for(Reservation  reserveI : reservations)
        {
            members.add(reserveI.getMember());
        }
        return members;
    }

    /*
     * check that item reserved by specific member
     */
    public boolean IsReservedBy(Member m)
    {
        for(Reservation res : reservations )
        {
            if(res.getMember() == m)
                return true;
        }
        return false;
    }

}
